package com.ken;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by renchongyu on 2018/12/1.
 */
@Configuration
public class MyConfig {

    /**
     * 将es整合到springboot中
     *
     */
    @Bean
    public TransportClient client() throws UnknownHostException{
        InetSocketTransportAddress node = new InetSocketTransportAddress(
                InetAddress.getByName("localhost"),9300
        );
        Settings settings = Settings.builder()
                .put("cluster.name","kenmaster")
                .build();
        TransportClient client = new PreBuiltTransportClient(settings);
        //支持多个node，只要new出来一次addTransportAddress进去就行
        client.addTransportAddress(node);
        return client;
    }
}
